﻿using System;
using System.IO;

namespace Gargoyle.Roton
{
    public class AudioPCMOut : Audio
    {
        private const string outFile = "output.raw";
        private FileStream fs;
        private BinaryWriter bw;

        public AudioPCMOut(int newSampleRate, BitDepth newBitsPerSample, int newBufferSize)
            : base(newSampleRate, newBitsPerSample, newBufferSize)
        {
            fs = new FileStream(outFile, FileMode.Create, FileAccess.Write, FileShare.Read);
            bw = new BinaryWriter(fs);

            base.BufferFull += AudioWaveOut_BufferFull;
        }

        ~AudioPCMOut()
        {
            base.BufferFull -= AudioWaveOut_BufferFull;

            bw.Close();
            fs.Close();
        }

        public void AudioWaveOut_BufferFull(object sender, BufferFullArgs e) {
            int bufferLength = (int)e.Buffer.Capacity;
            byte[] bufferData = new byte[bufferLength];
            e.Buffer.Read(bufferData, 0, bufferLength);
            bw.Write(bufferData);
        }
    }
}
